package com.example.triprec.ui.fragment;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;

import com.example.triprec.R;
import com.example.triprec.common.Result;
import com.example.triprec.dao.CollectDao;
import com.example.triprec.dao.TripDao;
import com.example.triprec.entity.User;
import com.example.triprec.ui.activity.LoginActivity;
import com.example.triprec.ui.activity.MyCollectionsActivity;
import com.example.triprec.ui.activity.MyCommentsActivity;
import com.example.triprec.utils.CurrentUserUtils;

public class MineFragment extends Fragment {

    // 顶部用户信息
    private TextView tvMineUsername;

    // 统计数据
    private TextView tvMineTripsCount;
    private TextView tvMineFavoritesCount;

    // 功能菜单
    private LinearLayout llMineComments;
    private LinearLayout llMineCollection;

    private Button btnLogout;

    private User currentUser;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_mine, container, false);
        initViews(view);
        // 设置点击事件
        setupListeners();
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        // 更新统计数据
        loadData();
    }

    private void initViews(View view) {
        // 初始化顶部用户信息
        tvMineUsername = view.findViewById(R.id.tv_mine_username);

        // 初始化统计数据
        tvMineTripsCount = view.findViewById(R.id.tv_mine_trips_count);
        tvMineFavoritesCount = view.findViewById(R.id.tv_mine_favorites_count);

        // 初始化功能菜单
        llMineComments = view.findViewById(R.id.ll_mine_comments);
        llMineCollection = view.findViewById(R.id.ll_mine_collection);

        btnLogout = view.findViewById(R.id.btn_logout);

        currentUser = CurrentUserUtils.getCurrentUser();

        // 设置用户信息
        tvMineUsername.setText(currentUser.getUsername());

    }

    private void loadData() {
        // 设置统计数据
        Result<Integer> tripCountResult = TripDao.getTripCountByUserId(currentUser.getId());
        if (tripCountResult.isSuccess()) {
            tvMineTripsCount.setText(String.valueOf(tripCountResult.getData()));
        } else {
            tvMineTripsCount.setText("0");
        }
        Result<Integer> favoritesCountResult = CollectDao.getCollectCountByUserId(currentUser.getId());
        if (favoritesCountResult.isSuccess()) {
            tvMineFavoritesCount.setText(String.valueOf(favoritesCountResult.getData()));
        } else {
            tvMineFavoritesCount.setText("0");
        }
    }

    private void setupListeners() {
        // 设置我的评论点击事件
        llMineComments.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), MyCommentsActivity.class);
                startActivity(intent);
            }
        });

        // 设置我的收藏点击事件
        llMineCollection.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), MyCollectionsActivity.class);
                startActivity(intent);
            }
        });
        
        btnLogout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog dialog = new AlertDialog.Builder(getContext())
                        .setTitle("确认退出登录吗？")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog1, int which) {
                                // 清除当前用户信息
                                CurrentUserUtils.clearCurrentUser();
                                // 返回登录界面
                                Intent intent = new Intent(getActivity(), LoginActivity.class);
                                startActivity(intent);
                                getActivity().finish();
                            }
                        })
                        .setNegativeButton("取消", null)
                        .create();
                dialog.show();
            }
        });
    }

} 